/**
 * Created by oneminute on 7/18/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();

describe("测试Commodity模块。", function () {
    var commodity01 = null;

    it("正常创建Commodity对象。", function (done) {
        commodity01 = new lz.Commodity({
            id: lz.Utils.getUUID("c_"),
            uid: "u_test0001",
            description: "测试描述属性",
            price: 10.19,
            photos: ["ph001", "ph002"]
        });
        commodity01.save(function (_err, _commodity) {
            testutils.check(done, function () {
                _commodity.uid.should.equal(commodity01.uid);
                assert.notEqual(_commodity._id, null);
            });
        });
    });

    it("根据_id正常加载Commodity对象。", function (done) {
        var commodity02 = new lz.Commodity({_id: commodity01._id});
        commodity02.load(function(_err, _commodity) {
            testutils.check(done, function () {
                commodity02.uid.should.equal(commodity01.uid);
            });
        });
    });

    it("根据AQL正常加载Commodity对象。", function (done) {
        var commodity03 = new lz.Commodity();
        commodity03.load({method: "byProp", prop: "uid", value: commodity01.uid}, function(_err, _commodity) {
            testutils.check(done, function() {
                commodity03.uid.should.equal(commodity01.uid);
            });
        });
    });

    it("根据uid使用list方法正常列出Commodity对象。", function (done) {
        var commodity04 = new lz.Commodity({uid: commodity01.uid});
        commodity04.list({method: "byProp", prop: "uid"}, function(_err, _commodities) {
            testutils.check(done, function() {
                assert.equal(_commodities.length > 0, true);
                lz.Logger.info("commodities: ", _commodities);
            });
        });
    });

    it("使用delete方法删除Commodity对象。", function (done) {
        commodity01.delete(function(_err){
            lz.ArangoDB.getObjectById(commodity01._id, function(_err, _obj) {
                testutils.check(done, function() {
                    assert.equal(_obj, null);
                });
            });
        });
    });

    after(function(done) {
        /*lz.ArangoDB.deleteObjectById(email01._id, function() {
            done();
        });*/
        done();
    });
});
